from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.conf import settings
from django.urls import reverse
from mdeditor.fields import MDTextField

# Create your models here.


# 点赞
class Likes(models.Model):
    """
    使用ContentType比单一的ForeignKey更灵活

    like_by: 记录点赞用户信息

    content_type: 设定一个普通外键，连接于ContentType，给对应的模型点赞

    object_id: 设立一个PostiveIntegerField的字段，一般名字叫做“object_id”。以记录所对应的模型的实例的id号。
               比如我们给一篇文章点赞，这篇文章是Post类里的id为10的文章，那么这个object_id就是这个10。
    content_object: 设定GenericForeignkey外键,需要传入两个参数content_type和object_id.

                    如果你为上面2个字段取的名字就是content_type和object_id的话，你可以不需要输入，因为这个字段默认会读取这2个名字。
                    如果你自定义过了，那就需要你手动添加。
    """

    like_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, default=1)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

    def __str__(self):
        return self.content_type.model.__str__()


# 文章类别
class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=500, verbose_name='分类名称', unique=True)
    parent = models.ForeignKey('self', default=None, null=True, blank=True,
                               verbose_name='上级分类', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    class Meta:
        verbose_name_plural = verbose_name = '分类'
        ordering = ['id']

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('blog:category', kwargs={'category': self.name})


# 文章标签
class Tag(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(verbose_name='标签', max_length=20, unique=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    class Meta:
        verbose_name_plural = verbose_name = '标签'
        ordering = ['id']

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('blog:tag', kwargs={'tag': self.name})


# 文章
class Article(models.Model):

    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.DO_NOTHING)
    category = models.ForeignKey(Category, verbose_name='分类', on_delete=models.SET_DEFAULT, default=1)
    title = models.CharField(verbose_name='标题', max_length=50)
    img = models.ImageField(verbose_name='封面图片', default='images/2.jpg', upload_to='images/')
    tag = models.ManyToManyField(Tag, verbose_name='标签')
    content = MDTextField()
    view_count = models.IntegerField(default=0)  # 阅读次数
    like_info = GenericRelation(Likes, related_query_name='articlemodel')
    is_top = models.BooleanField(default=False, verbose_name='置顶')
    is_original = models.BooleanField(default=False, verbose_name='是否原创')
    summary = models.TextField(verbose_name='摘要')
    pub_time = models.DateTimeField(verbose_name='发布时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    class Meta:
        verbose_name_plural = verbose_name = '文章'
        ordering = ['-pub_time']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:article', kwargs={'pk': self.id})


# 评论
class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    user_name = models.CharField(verbose_name='评论姓名', max_length=100)
    comment = models.TextField(verbose_name='评论内容')
    create_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)
    article = models.ForeignKey(Article, verbose_name='评论文章', on_delete=models.CASCADE)

    def __str__(self):
        return self.comment[:20]


# 导航栏
class Nav(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(verbose_name='导航栏标题', max_length=40)
    url = models.CharField(verbose_name='指向URL', max_length=200, blank=True, null=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now=True)
    rank = models.IntegerField(verbose_name='排序', default=0)

    class Meta:
        verbose_name_plural = verbose_name = "导航栏"
        ordering = ['-create_time']

    def __str__(self):
        return self.name


# 轮播图
class Carousel(models.Model):
    id = models.AutoField(primary_key=True)
    article = models.ForeignKey(Article, verbose_name='关联文章', on_delete=models.CASCADE)
    img = models.ImageField(verbose_name='封面图片', default='carousel/default.jpg', upload_to='carousel/')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


    class Meta:
        verbose_name_plural = verbose_name = "轮播图"
        ordering = ['-create_time']

    def __str__(self):
        return self.article.title


# 站长推荐
class Recommend(models.Model):
    id = models.AutoField(primary_key=True)
    article = models.ForeignKey(Article, verbose_name='推荐文章', on_delete=models.CASCADE)
    rank = models.IntegerField(verbose_name='排名', default=0)

    class Meta:
        verbose_name_plural = verbose_name = "推荐"
        ordering = ['-rank']

    def __str__(self):
        return self.article.title
